package com;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.lucene.util.RamUsageEstimator;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static java.math.BigDecimal.ROUND_HALF_UP;

/**
 * @author didi
 * @DESCRIPTION
 * @create 2019-05-04 15:23
 **/

public class Main {

    private static final double R = 6378137;

    //将球面距离公式保存在笔记中
    public static void main(String[] args) throws IOException, InterruptedException {
        File       file     = new File("/Users/didi/Documents/data_all/old");
        File[]     files    = file.listFiles();
        List<File> fileList = Lists.newArrayList();
        for (File ele : files) {
            if (ele.getName().endsWith("old")) {
                fileList.add(ele);
            }
        }

        Map<String, Double> map        = new TreeMap<>();
        PrintWriter         fileWriter = new PrintWriter(new File("/Users/didi/Documents/sourceFinalResultOld.txt"));

        for (File ele : fileList) {
            List<String> ll = new ArrayList<>();
            Files.lines(ele.toPath(), StandardCharsets.UTF_8).forEach((element) -> {
                ll.add(element);
            });
            double sumDistance = 0.0d;

            for (int index = 1; index < ll.size(); index++) {
                JSONObject lastHeart    = JSON.parseObject(ll.get(index - 1).toString());
                JSONObject currentHeart = JSON.parseObject(ll.get(index).toString());

                sumDistance += round(distanceSimplify(((BigDecimal) lastHeart.get("lng")).doubleValue(),
                                                      ((BigDecimal) lastHeart.get("lat")).doubleValue(),
                                                      ((BigDecimal) currentHeart.get("lng")).doubleValue(),
                                                      ((BigDecimal) currentHeart.get("lat")).doubleValue()), 0);


            }
            map.put(ele.getName().split("_")[0], sumDistance);
        }
        fileWriter.println(JSON.toJSONString(map));

        fileWriter.close();

        Thread thread = new Thread(() -> {
            for (long inde = 0; inde < Long.MAX_VALUE; inde++) {
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
                System.out.println(inde);
            }
        });
        thread.start();
        TimeUnit.MILLISECONDS.sleep(2);
        thread.interrupt();
        List<String> list = new ArrayList<>();
        for (int index = 0; index < 1; index++) {
            list.add(UUID.randomUUID().toString());
        }
        String result = RamUsageEstimator.humanSizeOf(list);
        System.out.println(result);




    }

    public static double round(double value, int prec) {
        if (prec < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(Double.toString(value));
        return b.setScale(prec, ROUND_HALF_UP).doubleValue();
    }

    public static double distanceSimplify(double lng1, double lat1, double lng2, double lat2) {
        if (Double.compare(lng1, lng2) == 0 && Double.compare(lat1, lng2) == 0) {
            return 0.0D;
        }

        double       dx = lng1 - lng2; // 经度差值
        double       dy = lat1 - lat2; // 纬度差值
        double       b  = (lat1 + lat2) / 2.0; // 平均纬度
        final double LX = toRadians(dx) * R * Math.cos(toRadians(b)); // 东西距离
        final double LY = R * toRadians(dy); // 南北距离
        return Math.sqrt(LX * LX + LY * LY); // 用平面的矩形对角距离公式计算总距离
    }

    /**
     * 转换成弧度
     *
     * @param dx
     * @return
     */
    private static double toRadians(double dx) {
        return dx * Math.PI / 180;
    }

}
